﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.ArticleManage
{
	public partial class AddArticle : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
					Page.DataBind();
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (IsValid)
			{
				Article info = UCArticle.Info;				

				if (ModuleId.HasValue)	// 发布到栏目
				{
					if (RightValidate(ModuleId.Value))
					{
						ModuleArticle mInfo = ModuleArticles.Release(info, ModuleId.Value);
						InsertComplete(mInfo.ArticleId);
					}
				}
				else
				{
					if (RightValidate())
					{
						info = Articles.Insert(info);
						InsertComplete(info.ArticleId);
					}
				}
				
			}
		}

		/// <summary>
		/// 对文章添加进行权限验证，并返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Articles.GetInsertRight(new Article());
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 对文章发布进行权限验证，并返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <param name="moduleID">进行验证的模块ID。</param>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate(int moduleID)
		{
			bool right = ModuleArticles.GetReleaseRight(new ModuleArticle { ModuleId = moduleID });
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 插入操作完成，显示消息提示。
		/// </summary>
		public void InsertComplete(int articleId)
		{
			Messages.Add(new PageMessage(false, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Insert, "ModifyArticle.aspx?ArticleID=" + articleId, Res.OperationComplete_GoToModifyText));
		}

		protected void BtReload_Click(object sender, EventArgs e)
		{
			DataBind();
		}

	}
}
